<?php
function generate_lageplan($coordinates) {
	//print_r($coordinates);
	$width = 400;
	$height = 400;
	$image = @imagecreate($width, $height);
	$bg_color = imagecolorallocate($image, 204 , 204, 204);
	$text_color = imagecolorallocate($image, 0, 51, 102);
	$start_color = imagecolorallocate($image, 0 , 100, 255);
	imagefill($image, 5, 5, $bg_color);
	$breite_min = 9999;
	$breite_max = 0;
	$laenge_min = 9999;
	$laenge_max = 0;
	$diff_breite = 0;
	$diff_laenge = 0;
	$startpunkt;
	//Max und min ermitteln
	foreach($coordinates as $dataset) {
		$long = $dataset[2];
		$lat = $dataset[3];
		$name = $dataset[0];
		$istStartpunkt = $dataset[6];
		if($lat > $breite_max) {$breite_max = $lat;}
		if($long > $laenge_max) {$laenge_max = $long;}
		if($lat < $breite_min) {$breite_min = $lat;}
		if($long < $laenge_min) {$laenge_min = $long;}
		if($istStartpunkt = 1) {$startpunkt = $dataset;}
	}
	$diff_breite = $breite_max - $breite_min;
	$diff_laenge = $laenge_max - $laenge_min;
	$diff_max = 0;
	if($diff_breite>$diff_laenge){$diff_max = $diff_breite;} else {$diff_max = $diff_laenge;};
	//Punkte einzeichnen
	$y_min_range = 0.1 * $height;
	$x_min_range = 0.1 * $width;
	$y_max_range = 0.9 * $height;
	$x_max_range = 0.9 * $width;
	$diff_x_range = $x_max_range-$x_min_range;
	$diff_y_range = $y_max_range-$y_min_range;
	$diff_max_range = 0;
	if($diff_x_range>$diff_y_range){$diff_max_range = $diff_x_range;} else {$diff_max_range = $diff_y_range;}
	
	foreach($coordinates as $dataset) {
		$long = $dataset[2];
		$lat = $dataset[3];
		$name = $dataset[0];
		$istStartpunkt = $dataset[6];
		$x = $x_min_range+($long-$laenge_min)/$diff_max*$diff_max_range;
		$y = $y_min_range+($lat-$breite_min)/$diff_max*$diff_max_range;

		if($istStartpunkt == 1) {
			imagefilledellipse($image, $x , $height-$y, 10, 10, $start_color);
		} else {
			imagefilledellipse($image, $x , $height-$y, 10, 10, $text_color);
		}
	}

	$filename = "lageplan.png";
	imagepng($image, $filename);	
	echo "<br /><img src=\"".$filename."\" /> ";
	return $filename;
}


?>